import csv

import jsonpath
import requests

list = []


def main():
    ip = "114.99.221.148"
    port = 4345

    proxies = {
        "http": "http://%s:%d" % (ip, port),
        "https": "http://%s:%d" % (ip, port)
    }

    headers = {
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
    }

    r = requests.post(url=url, headers=headers, proxies=proxies)

    json_dict = r.json()

    json_count = jsonpath.jsonpath(json_dict, "$..props")

    for i in json_count:
        for y in i:
            count = {}
            count["region_name"] = y['region_name']
            count["block_name"] = y['block_name']
            count["rhval"] = y['rhval']
            count["area"] = y['area']
            count["price"] = y['price']
            count["long_title"] = y['long_title']
            count["comm_name"] = y['comm_name']
            count["house_orient_name"] = y['house_orient_name']
            count["floor_tag"] = y['floor_tag']
            list.append(count)


def write():
    with open('沭阳房产_20201106.csv', 'w+', encoding='utf-8') as f:
        f_csv = csv.DictWriter(f, ['region_name', 'block_name', 'rhval', 'area', 'price', 'long_title',
                                   'comm_name', 'house_orient_name', 'floor_tag'])
        f_csv.writeheader()
        f_csv.writerows(list)


if __name__ == '__main__':
    for num in range(2):
        url = "https://shuyang.anjuke.com/v3/ajax/map/sale/1851/prop_list/?room_num=-1&price_id=-1&area_id=-1&floor=-1&orientation=-1&is_two_years=0&is_school=0&is_metro=0&order_id=0&p=%s&zoom=12&lat=34.137058_34.275585&lng=118.368882_119.251952&kw=&et=MjFmMz&ib=1&bst=pem60" % (
                num + 1)
        main()
    write()
